package com.sbtr.business.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sbtr.business.system.dto.DictionaryTreeDto;
import com.sbtr.business.system.entity.Dictionary;
import com.sbtr.business.system.dto.DictionaryDto;

import com.sbtr.business.system.entity.DictionaryParam;
import com.sbtr.business.system.service.DictionaryParamService;
import com.sbtr.business.system.service.DictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.business.system.mapper.DictionaryMapper;
import com.sbtr.util.BeanCopyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.common.PageDto;
import com.sbtr.util.PageDtoUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典字段列表 Service接口实现类
 *
 * @author dgl
 * @since 2023-03-22
 */
@Transactional
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Autowired
    DictionaryMapper dictionaryMapper;
    @Autowired
    private DictionaryParamService dictionaryParamService;

    @Override
    public PageDto<Dictionary> pageList(DictionaryDto dictionaryDto) {

        IPage<Dictionary> page = new Page<>();
        page.setSize(dictionaryDto.getPageSize());
        page.setCurrent(dictionaryDto.getPage());
        page = dictionaryMapper.selectPages(page, BeanUtil.beanToMap(dictionaryDto));
        PageDto<Dictionary> r = PageDtoUtils.getPageDtoByIPage(page, Dictionary.class);

        return r;
    }

    @Override
    public List<DictionaryTreeDto> treeList(String dirCodes, Dictionary dictionary) {
        QueryWrapper<Dictionary> dictionaryWrapper = new QueryWrapper<>(dictionary);
        if (StringUtils.isNotBlank(dirCodes)) {
            dictionaryWrapper.likeRight("dir_code", dirCodes);
        }
        dictionaryWrapper.orderByAsc("number");
        List<Dictionary> list = this.list(dictionaryWrapper);
        List<Long> ids = new ArrayList<>();

        for (Dictionary dictionary1 : list) {
            ids.add(dictionary1.getId());
        }

        QueryWrapper<DictionaryParam> dictionaryParamWrapper = new QueryWrapper<>();
        dictionaryParamWrapper.in("dir_id", ids);
        dictionaryParamWrapper.orderByAsc("number");

        //迭代二级制作嵌套菜单结构
        List<DictionaryParam> dictionaryParamList = dictionaryParamService.list(dictionaryParamWrapper);
        List<DictionaryTreeDto> treeDtos = new ArrayList<>();
        for (Dictionary dictionary1 : list) {//迭代设置字典选项内容
            DictionaryTreeDto dictionaryTreeDto = BeanCopyUtils.copyProperties(dictionary1, DictionaryTreeDto.class);
            dictionaryTreeDto.setChildrenParams(new ArrayList<>());
            for (DictionaryParam dictionaryParam : dictionaryParamList) {
                if (dictionaryParam.getDirId().equals(dictionary1.getId())) {
                    dictionaryTreeDto.getChildrenParams().add(dictionaryParam);
                }
            }
            treeDtos.add(dictionaryTreeDto);
        }
        return treeDtos;
    }
}

